package com.team.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.team.entity.Idea;
import com.team.entity.Project;
import com.team.util.DbHelp;
import com.team.util.ResultSetPackge;

public class IdeaDao {
	private DbHelp<Idea> db = new DbHelp<Idea>();
	
	public int save(Idea idea){
		String sql ="insert into t_idea(title,content,createtime,t_user_id,t_project_id) values(?,?,?,?,?)";
		return db.executeUpdateSql(sql, idea.getTitle(),idea.getContent(),idea.getCreateTime(),idea.getUserId(),idea.getProjId());
	}
	public List<Idea> selectAll(){
		String sql="select id,title,content,createtime,t_user_id,t_project_id from t_idea";
		return db.exeSelectForList(sql,new PackgeIdea());
	}

	public List<Idea> selectAllByProjId(int projId) {
		String sql= "select id,title,content,createtime,t_user_id,t_project_id from t_idea where t_project_id = ?";
		return db.exeSelectForList(sql,new PackgeIdea(), projId);
	
	}
	public Idea selectByIdeaId(int id) {
		String sql= "select id,title,content,createtime,t_user_id,t_project_id from t_idea where id = ?";
		return db.exeSelectForObject(sql, new PackgeIdea(), id);
		
	}
	


	public int updateIdea(String title, String content, int id) {
		String sql = "update t_idea set title=?,content=? where id=?";
		return db.executeUpdateSql(sql, title,content,id);
	
	}
	
	private class PackgeIdea implements ResultSetPackge<Idea>{

		public Idea packgeResultSet(ResultSet rs) throws SQLException {
			Idea idea = new Idea();
			
			idea.setId(rs.getInt("id"));
			idea.setCreateTime(rs.getString("createtime"));
			idea.setProjId(rs.getInt("t_project_id"));
			idea.setTitle(rs.getString("title"));
			idea.setUserId(rs.getInt("t_user_id"));
			idea.setContent(rs.getString("content"));
			return idea;
		}
		
	}
	 
}
